<?php

/**
 * This is the view file.
 * It contacts the control file to manipulate data
 * 
 */
echo "<html><body>";
include '../db-control/inc/functions.php';
include '../model/Contact.php';
include '../db-control/dbContact.php';

//update a contact if the user is trying to do so
if (isset($_POST['update']) && $_POST['update'] != '') {
    $id = $_POST['update'];
    $c = new Contact(); //creates a new instance of Contact()
    $c->setID($id); //sets the contact ID
    $db = new dbContact();
    $exists = $db->checkID($c); //checks to see if the ID is a real contact
    if (!$exists) {
        echo "<b>Contact with ID '$id' does not exist...</b>";
    } else {
        $c->setFName($_POST['fname']); //sets the contact first name
        $c->setLName($_POST['lname']); //sets the contact last name
        $result = $db->updateContact($c); //finally, updates the contact with the data we've set
        if (!$result) {
            echo "<b>Could not update user with ID '$id'...</b>";
        }
    }
} else {
    if (isset($_POST['add'])) {
        $c = new Contact();
        $c->setFName($_POST['fname']);
        $c->setLName($_POST['lname']);
        $db = new dbContact();
        $result = $db->addContact($c);
        if (!$result) {
            echo "<b>Please enter some contact information</b>";
        }
    }

    if (isset($_POST['deleteButton'])) {
        $c = new Contact(); //creates a new Contact object (i.e. creates a new contact)
        $c->setID($_POST['deleteID']);
        $db = new dbContact();
        $result = $db->deleteContact($c);
        if (!$result) {
            echo "<b>Could not delete contact with ID '$_POST[deleteID]'...</b>";
        }
    }
}


//displays the web form used for adding a new contact
echo <<<_FORM


<form action='viewContact.php' method='post'>
<table>
    <tr><td colspan='2'>Specify an ID to change a contact's data</td><td><input type='text' size='4' name='update' /></td></tr>
    <tr><td>First Name:</td><td><input type='text' value='' name='fname' /></td></tr>
    <tr><td>Last Name:</td><td><input type='text' value='' name='lname' /></td></tr>
    <tr><td></td><td align='right'><input type='submit' value='Add Contact' name='add'/></td></tr>
</table>    
</form>


_FORM;

displayContacts(); //displays the contact list


echo "</body></html>";

function displayContacts() {
    $db = new dbContact(); //gets all the contacts from the database
    $result = $db->getAllContacts();

    echo "<table width='200px'>";
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $id = $row['id'];
        $fname = $row['fname'];
        $lname = $row['lname'];
        echo "<tr><td>$id</td><td></td></tr>";
        echo "<tr><td align='right'>First Name:</td><td align='left'>$fname</td></tr>";
        echo "<tr><td align='right'>Last Name:</td><td align='left'>$lname</td></tr>";
        echo "<tr><td colspan='2' align='right'><form action='viewContact.php' method='post'><input type='hidden' value='$id' name='deleteID' /><input type='submit' value='Delete' name='deleteButton' /></form></td><td></td></tr>";
        echo "<tr><td colspan='2'><hr /></td><td></td></tr>";
    }
    echo "</table>";
}
?>

